package CustomUI;

import java.util.ArrayList;

import org.Consts;
import org.Globals;
import org.sqlStatements;

import com.example.dworld3.Draw;

import DTO.DrawingActionDTO;
import DTO.DrawingPlaneDTO;
import DTO.ObjectDTO;
import DTO.Properties;
import DTO.PropertiesDTO;
import GameTools.ydMath;
import GameTools.ydPoint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Cap;
import android.graphics.Paint.Join;
import android.graphics.Path;
import android.graphics.Paint.Style;
import android.graphics.Path.Direction;
import android.graphics.Matrix;
import android.graphics.PathMeasure;
import android.graphics.PixelFormat;
import android.graphics.Point;
import android.graphics.PorterDuff;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.graphics.PorterDuff.Mode;

public class TouchView extends SurfaceView implements SurfaceHolder.Callback {

	private TouchViewThread thread;

	public ArrayList<DrawingPlaneDTO> drawingPlanes;

	DrawingPlaneDTO drawingPlane;

	DrawingActionDTO drawingAction;

	ydPoint canvasSize;

	public String currentAction = sqlStatements.DC_BRUSH;// Consts.DRAWING_ACTIONS_BRUSH;
	int currentColor = Color.GREEN;
	int currentStrokeWidth = 7;

	int currentStep = 0;

	Path currentPath = null;
	Matrix matrix = null;

	ArrayList<ydPoint> currentPathPoints;

	String currentShape = Consts.DRAWING_SHAPE_RECTANGLE;

	String currentText = "Nothing";

	private Bitmap mBitmap;
	private Canvas mCanvas;

	private Paint mBitmapPaint;

	Paint paint;
	ArrayList<Point> points;
	Point newPoint = new Point(0, 0);
	Point oldPoint = new Point(0, 0);

	ArrayList<Path> paths;
	ArrayList<Integer> colors;

	Context mContext;
	Properties currentProps;

	int gridH = 0;
	int gridV = 0;
	Boolean snap = false;
	Boolean isGridSym = false;

	public TouchView(Context context) {
		super(context);
		init(context);
	}

	public TouchView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public TouchView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context);
	}

	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
	}

	public void surfaceCreated(SurfaceHolder holder) {
		thread.setRunning(true);
		thread.start();
	}

	public void surfaceDestroyed(SurfaceHolder holder) {
		// Log.d(TAG, �Surface is being destroyed�);
		// tell the thread to shut down and wait for it to finish
		// this is a clean shutdown
		boolean retry = true;
		while (retry) {
			try {
				thread.setRunning(false);
				thread.join();
				// (get).finish();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			retry = false;
		}
	}

	public void init(Context context) {

		mContext = context;

		getHolder().addCallback(this);
		setFocusable(true);

		thread = new TouchViewThread(getHolder(), this);

		paint = new Paint();
		paint.setColor(currentColor);
		paint.setStyle(Style.STROKE);
		points = new ArrayList<Point>();

		paths = new ArrayList<Path>();
		colors = new ArrayList<Integer>();

		drawingPlanes = new ArrayList<DrawingPlaneDTO>();

		clearCanvas();

	}

	public void clearCanvas() {
		if (!isInEditMode()) {
			setZOrderOnTop(true); // necessary
			SurfaceHolder sfhTrackHolder = getHolder();
			sfhTrackHolder.setFormat(PixelFormat.TRANSPARENT);
		}
	}

	/*
	 * @Override protected void onSizeChanged(int w, int h, int oldw, int oldh)
	 * { super.onSizeChanged(w, h, oldw, oldh); canvasSize = new ydPoint(w, h);
	 * mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); mCanvas =
	 * new Canvas(mBitmap); }
	 */

	Paint gridPaint = new Paint();

	@Override
	public void draw(Canvas canvas) {

		super.draw(canvas);

		canvas.drawColor(0, PorterDuff.Mode.CLEAR);
		// canvas.drawColor(Color.YELLOW);

		gridPaint.setColor(Color.BLACK);
		gridPaint.setStrokeWidth(1);

		if (isGridSym && (gridH > 0 || gridV > 0)) {
			float divSizeH = 0;
			if (gridH > 0) {
				divSizeH = getHeight() / (gridH + 1);
			}

			float divSizeV = 0;

			if (gridV > 0) {
				divSizeV = getWidth() / (gridV + 1);
			}

			if (gridH == 0) {
				gridH = (int) ((getHeight() / divSizeV) - 1);
			} else if (gridV == 0) {
				gridV = (int) ((getWidth() / divSizeH) - 1);
			} else if (divSizeH > divSizeV) {
				gridH = (int) ((getHeight() / divSizeV) - 1);
			} else {
				gridV = (int) ((getWidth() / divSizeH) - 1);
			}
		}

		if (gridH > 0) {
			int width = getWidth();
			float divSize = getHeight() / (gridH + 1);
			float dy = 0;
			for (int i = 0; i <= gridH; i++) {
				dy = divSize * (i + 1);
				canvas.drawLine(0, dy, width, dy, gridPaint);
			}
		}

		if (gridV > 0) {
			int height = getHeight();
			float divSize = getWidth() / (gridV + 1);
			float dx = 0;
			for (int i = 0; i <= gridV; i++) {
				dx = divSize * (i + 1);
				canvas.drawLine(dx, 0, dx, height, gridPaint);
			}
		}

		if (prev != null) {
			canvas.drawCircle(prev.x, prev.y, 5, gridPaint);
		}

		if (currentPath != null) {
			canvas.drawPath(currentPath, paint);
		} else {
			canvas.drawCircle(20, 20, 12, paint);
		}
		paint.setTextSize(100);

		/*  canvas.drawText(origin.toString(), 10, 100, paint);
		  canvas.drawText(movedTo.toString(), 10, 300, paint);
		  if(thirdPoint!=null){
			  canvas.drawText(thirdPoint.toString(), 10, 400, paint);
			  double b = ydMath.cosineLaw(origin, movedTo, thirdPoint);

				 canvas.drawText(ydMath.radsToDegrees((float) b)+"", 10, 500, paint);
		  }*/
		 

	}

	int i = 0;
	ydPoint previous1 = new ydPoint(-100, 0);
	ydPoint previous2 = new ydPoint();

	ydPoint origin = new ydPoint();
	ydPoint movedTo = new ydPoint();
	ydPoint thirdPoint=null;
	ydPoint sym = new ydPoint();

	float px = 0;
	float py = 0;

	ydPoint prev = null;

	@Override
	public boolean onTouchEvent(MotionEvent event) {

		float x = event.getX();
		float y = event.getY();

		if (currentProps.getAction().equals(sqlStatements.DC_BRUSH)) {
			if (event.getAction() == MotionEvent.ACTION_DOWN) {
				if (prev == null) {
					prev = new ydPoint();
				}
				if (snap) {
					ydPoint p = new ydPoint(x, y);
					snapPoint(p);
					x = p.x;
					y = p.y;

					if (currentPath == null) {
						((Draw) mContext).touchedStarted();
						currentPath = new Path();
						matrix = new Matrix();
						currentPath.reset();
						currentPath.moveTo(x, y);
					} else
						currentPath.lineTo(x, y);
				}

				else {
					if (currentPath != null) {

						return false;
					}
					((Draw) mContext).touchedStarted();
					currentPath = new Path();
					matrix = new Matrix();
					touch_start(x, y);
				}
				prev.setCoordinates(x, y);

			} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
				if (snap) {
					ydPoint p = new ydPoint(x, y);
					snapPoint(p);
					x = p.x;
					y = p.y;


					currentPath.lineTo(x, y);
				} else {
					touch_move(x, y);
				}

				prev.setCoordinates(x, y);

			} else if (event.getAction() == MotionEvent.ACTION_UP) {
				if (!snap) {
					touch_up();
				}
				RectF bounds = new RectF();
				currentPath.computeBounds(bounds, true);
				px = (bounds.right - bounds.left) / 2;
				py = (bounds.bottom - bounds.top) / 2;
				clearCanvas();
				paint.setAlpha(128);
				((Draw) mContext).transformTouchViewControls();
				((Draw) mContext).touchedEnded();
				/*
				 * currentPath = null; clearCanvas();
				 */
				i++;
			}
		}

		else if (currentProps.getAction().equals(sqlStatements.DC_SHAPE)) {
			// if(currentProps.getShape().equals(sqlStatements.DC_RECTANGLE))
			if (event.getAction() == MotionEvent.ACTION_DOWN) {
				if (snap) {
					ydPoint p = new ydPoint(x, y);
					snapPoint(p);
					x = p.x;
					y = p.y;
				}

					if (!currentProps.shape.equals(sqlStatements.DC_ARC)) {

						((Draw) mContext).touchedStarted();
						currentPath = new Path();
						matrix = new Matrix();
						origin.setCoordinates(x, y);
						movedTo.setCoordinates(x, y);
						sym.setCoordinates(x, y);
						drawPathFor(origin, movedTo, null, currentProps);
					} else {
						if(currentPath==null){
							((Draw) mContext).touchedStarted();
							currentPath = new Path();
							matrix = new Matrix();
							origin.setCoordinates(x, y);
							movedTo.setCoordinates(x, y);
							sym.setCoordinates(x, y);
							drawPathFor(origin, movedTo, null, currentProps);
						}
						else{
							thirdPoint=new ydPoint(x, y);
							drawPathFor(origin, movedTo, thirdPoint, currentProps);
						}
						
					}
				} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
					if (snap) {
						ydPoint p = new ydPoint(x, y);
						snapPoint(p);
						x = p.x;
						y = p.y;

					}
					if(thirdPoint==null){
					movedTo.setCoordinates(x, y);
					sym.setCoordinates(x, y);
					drawPathFor(origin, movedTo, null, currentProps);
					}
					else{
						thirdPoint.setCoordinates(x, y);
						sym.setCoordinates(x, y);
						drawPathFor(origin, movedTo, thirdPoint, currentProps);
					}
				} else if (event.getAction() == MotionEvent.ACTION_UP) {
					if (snap) {
						ydPoint p = new ydPoint(x, y);
						snapPoint(p);
						x = p.x;
						y = p.y;

					}
					clearCanvas();
					paint.setAlpha(128);
					if(thirdPoint==null){
						movedTo.setCoordinates(x, y);
						sym.setCoordinates(x, y);
						drawPathFor(origin, movedTo, null, currentProps);
						}
						else{
							thirdPoint.setCoordinates(x, y);
							sym.setCoordinates(x, y);
							drawPathFor(origin, movedTo, thirdPoint, currentProps);
						}
					
					((Draw) mContext).transformTouchViewControls();
					((Draw) mContext).touchedEnded();

				}

			}

		if (currentProps.getAction().equals(sqlStatements.DC_ERASE)) {
			if (event.getAction() == MotionEvent.ACTION_DOWN) {
				if (currentPath != null) {
					return false;
				}
				if (prev == null) {
					prev = new ydPoint();
				}
				if (snap) {

					float divV = getWidth() / (gridV + 1);
					float divH = getHeight() / (gridH + 1);

					float lv1 = (float) Math.floor(x / divV);
					float lv2 = lv1 + 1;

					if (Math.abs(lv1 * divV - x) < Math.abs(lv2 * divV - x)) {
						x = lv1 * divV;
					} else {
						x = lv2 * divV;
					}

					float lh1 = (float) Math.floor(y / divH);
					float lh2 = lh1 + 1;

					if (Math.abs(lh1 * divH - y) < Math.abs(lh2 * divH - y)) {
						y = lh1 * divH;
					} else {
						y = lh2 * divH;
					}

					if (currentPath == null) {
						((Draw) mContext).touchedStarted();
						currentPath = new Path();
						matrix = new Matrix();
						currentPath.reset();
						currentPath.moveTo(x, y);
					} else
						currentPath.lineTo(x, y);
				}

				else {
					if (currentPath != null) {

						return false;
					}
					((Draw) mContext).touchedStarted();
					currentPath = new Path();
					matrix = new Matrix();
					touch_start(x, y);
				}
				((Draw) mContext).erase(currentPath);
				prev.setCoordinates(x, y);

			} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
				if (snap) {
					float divV = getWidth() / (gridV + 1);
					float divH = getHeight() / (gridH + 1);

					float lv1 = (float) Math.floor(x / divV);
					float lv2 = lv1 + 1;

					if (Math.abs(lv1 * divV - x) < Math.abs(lv2 * divV - x)) {
						x = lv1 * divV;
					} else {
						x = lv2 * divV;
					}

					float lh1 = (float) Math.floor(y / divH);
					float lh2 = lh1 + 1;

					if (Math.abs(lh1 * divH - y) < Math.abs(lh2 * divH - y)) {
						y = lh1 * divH;
					} else {
						y = lh2 * divH;
					}

					currentPath.lineTo(x, y);
				} else {
					touch_move(x, y);
				}
				((Draw) mContext).erase(currentPath);
				prev.setCoordinates(x, y);

			} else if (event.getAction() == MotionEvent.ACTION_UP) {
				if (!snap) {
					touch_up();
				}
				RectF bounds = new RectF();
				currentPath.computeBounds(bounds, true);
				px = (bounds.right - bounds.left) / 2;
				py = (bounds.bottom - bounds.top) / 2;
				clearCanvas();
				// paint.setAlpha(128);
				// ((Draw) mContext).transformTouchViewControls();
				((Draw) mContext).touchedEnded();
				((Draw) mContext).showHideTransformationTools(View.GONE);
			}
		}

		else if (currentAction.equals(Consts.DRAWING_ACTIONS_TEXT)) {
			if (event.getAction() == MotionEvent.ACTION_DOWN) {
				drawingPlane = new DrawingPlaneDTO(canvasSize);
				drawingAction = new DrawingActionDTO(currentAction,
						currentColor, currentStrokeWidth);
				drawingAction.setString(currentText);
				touch_start(x, y);
				drawingPlane.addAction(drawingAction);
				drawingPlanes.add(drawingPlane);
			} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
				touch_move(x, y);
			} else if (event.getAction() == MotionEvent.ACTION_UP) {
				touch_up();
				if (new PathMeasure(drawingAction.path, false).getLength() > 3) {
					i++;
				} else {
					drawingPlanes.remove(i);
				}
			}
			drawingPlane.calculateBounds();
		} else if (currentAction.equals(Consts.DRAWING_ACTIONS_ERASE)) {
			if (event.getAction() == MotionEvent.ACTION_DOWN) {
				drawingAction = new DrawingActionDTO(currentAction,
						currentColor, currentStrokeWidth);
				touch_start(x, y);
				for (DrawingPlaneDTO d : drawingPlanes) {
					d.addAction(drawingAction);
				}
				// drawingPlane.addAction(drawingAction);
				// paths.add(i, path);
			} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
				touch_move(x, y);

			} else if (event.getAction() == MotionEvent.ACTION_UP) {
				touch_up();
				for (DrawingPlaneDTO d : drawingPlanes) {
					d.setAction(drawingAction.copy());
				}
			}
		}

		invalidate();
		return true;

	}

	private void snapPoint(ydPoint p) {
		float divV = getWidth() / (gridV + 1);
		float divH = getHeight() / (gridH + 1);

		float lv1 = (float) Math.floor(p.x / divV);
		float lv2 = lv1 + 1;

		if (Math.abs(lv1 * divV - p.x) < Math.abs(lv2 * divV - p.x)) {
			p.x = lv1 * divV;
		} else {
			p.x = lv2 * divV;
		}

		float lh1 = (float) Math.floor(p.y / divH);
		float lh2 = lh1 + 1;

		if (Math.abs(lh1 * divH - p.y) < Math.abs(lh2 * divH - p.y)) {
			p.y = lh1 * divH;
		} else {
			p.y = lh2 * divH;
		}

	}

	private void drawPathFor(ydPoint origin2, ydPoint movedTo2, ydPoint p,
			Properties props) {

		currentPath.reset();

		if (props.sym == 1) {
			float dX = (movedTo2.x - origin2.x);
			float dY = (movedTo2.y - origin2.y);

			if (Math.abs(dX) < Math.abs(dY)) {
				// movedTo2.x=origin2.x+(movedTo2.x/Math.abs(movedTo2.x))*(movedTo2.y-origin2.y);
				movedTo2.x = origin2.x + (dX / Math.abs(dX))
						* Math.abs(movedTo2.y - origin2.y);
			} else {
				// movedTo2.y=origin2.y+(movedTo2.y/Math.abs(movedTo2.y))*-(movedTo2.x-origin2.x);
				movedTo2.y = origin2.y + (dY / Math.abs(dY))
						* Math.abs(movedTo2.x - origin2.x);
			}
		}

		if (props.shape.equals(sqlStatements.DC_RECTANGLE)) {
			currentPath.addRect(origin2.x, origin2.y, movedTo2.x, movedTo2.y,
					Direction.CW);
		} else if (props.shape.equals(sqlStatements.DC_CIRCLE)) {
			// origin2.setCoordinates(300, 300);
			// movedTo2.setCoordinates(50, 50);
			currentPath.addOval(new RectF(origin2.x, origin2.y, movedTo2.x,
					movedTo2.y), Direction.CW);
		} else if (props.shape.equals(sqlStatements.DC_LINE)) {
			currentPath.moveTo(origin2.x, origin2.y);
			currentPath.lineTo(movedTo2.x, movedTo2.y);
		} else if (props.shape.equals(sqlStatements.DC_ARC)) {
			if (p == null) {
				currentPath.moveTo(origin2.x, origin2.y);
				currentPath.lineTo(movedTo2.x, movedTo2.y);
			} else {
				double b = ydMath.cosineLaw(origin2, p, movedTo2);
				currentPath.addArc(new RectF(origin2.x, origin2.y, movedTo2.x,
						movedTo2.y), 0, ydMath.radsToDegrees((float) b));
			}
		}

	}

	private float mX, mY;
	private static final float TOUCH_TOLERANCE = 4;

	private void touch_start(float x, float y) {
		currentPath.reset();
		currentPath.moveTo(x, y);
		// drawingAction.pathPoints.add(new ydPoint(x, y));
		mX = x;
		mY = y;
	}

	private void touch_move(float x, float y) {
		float dx = Math.abs(x - mX);
		float dy = Math.abs(y - mY);
		if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
			currentPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
			mX = x;
			mY = y;
			// drawingAction.pathPoints.add(new ydPoint(x, y));
		}
	}

	private void touch_up() {
		currentPath.lineTo(mX, mY);
		// drawingAction.pathPoints.add(new ydPoint(mX, mY));
	}

	public void ConfirmClicked() {
		prev = null;
		paint.setAlpha(255);
		((Draw) mContext).addCMD(currentPath);
		currentPath = null;
		clearCanvas();
		thirdPoint=null;
	}

	public void CancelClicked() {
		prev = null;
		currentPath = null;
		clearCanvas();
		thirdPoint=null;
	}

	public void setColor(int color) {
		currentColor = color;

	}

	public void setAction(String action) {
		currentAction = action;

	}

	public void setShape(String shape) {
		currentShape = shape;

	}

	public void doneAction() {
		i++;
	}

	public void directionClicked(String direction) {

		ydPoint shift = new ydPoint();

		if (direction.equals(Consts.DIRECTION_NW)) {
			shift.setCoordinates(-1f, -1f);
		} else if (direction.equals(Consts.DIRECTION_N)) {
			shift.setCoordinates(0, -1f);
		} else if (direction.equals(Consts.DIRECTION_NE)) {
			shift.setCoordinates(1f, -1f);
		} else if (direction.equals(Consts.DIRECTION_W)) {
			shift.setCoordinates(-1f, 0);
		} else if (direction.equals(Consts.DIRECTION_E)) {
			shift.setCoordinates(1f, 0);
		} else if (direction.equals(Consts.DIRECTION_SW)) {
			shift.setCoordinates(-1f, 1f);
		} else if (direction.equals(Consts.DIRECTION_S)) {
			shift.setCoordinates(0, 1f);
		} else if (direction.equals(Consts.DIRECTION_SE)) {
			shift.setCoordinates(1f, 1f);
		}
		drawingAction.deltaMove(shift);
		invalidate();

	}

	public void cancelAction() {
		drawingPlanes.remove(i);
	}

	public void undo() {
		if (i > 0) {
			i--;
			// drawingAction=drawingActions.get(i);
			invalidate();
		}
	}

	public void deltaMove(ydPoint shift, int actionMove) {
		if (actionMove == MotionEvent.ACTION_DOWN) {
			drawingAction = new DrawingActionDTO(Consts.DRAWING_ACTIONS_MOVE,
					currentColor, currentStrokeWidth);
			drawingAction.move(shift);
			drawingPlane.addAction(drawingAction);
		} else if (actionMove == MotionEvent.ACTION_MOVE) {
			drawingAction.move(shift);
		} else if (actionMove == MotionEvent.ACTION_UP) {
			drawingAction.move(shift);
		}
		invalidate();
	}

	public void deltaRotate(ydPoint shift, int actionMove) {
		if (actionMove == MotionEvent.ACTION_DOWN) {
			drawingAction = new DrawingActionDTO(Consts.DRAWING_ACTIONS_MOVE,
					currentColor, currentStrokeWidth);
			drawingAction.move(shift);
			drawingPlane.addAction(drawingAction);
		} else if (actionMove == MotionEvent.ACTION_MOVE) {
			drawingAction.move(shift);
		} else if (actionMove == MotionEvent.ACTION_UP) {
			drawingAction.move(shift);
		}
		invalidate();
	}

	public void deltaScale(ydPoint shift, int actionMove) {
		if (actionMove == MotionEvent.ACTION_DOWN) {
			drawingAction = new DrawingActionDTO(Consts.DRAWING_ACTIONS_MOVE,
					currentColor, currentStrokeWidth);
			drawingAction.move(shift);
			drawingPlane.addAction(drawingAction);
		} else if (actionMove == MotionEvent.ACTION_MOVE) {
			drawingAction.move(shift);
		} else if (actionMove == MotionEvent.ACTION_UP) {
			drawingAction.move(shift);
		}
		invalidate();
	}

	public void deltaTransform(ydPoint shift, int motionEvent) {
		if (currentAction.equals(Consts.DRAWING_ACTIONS_MOVE)) {
			if (motionEvent == MotionEvent.ACTION_DOWN) {
				drawingAction = new DrawingActionDTO(currentAction,
						currentColor, currentStrokeWidth);
				drawingPlane.addAction(drawingAction);
			} else if (motionEvent == MotionEvent.ACTION_MOVE) {

				drawingAction.deltaMove(shift);

			} else if (motionEvent == MotionEvent.ACTION_UP) {
				drawingAction.deltaMove(shift);
			}
			drawingPlane.boundBR.deltaMove(shift);
			drawingPlane.boundTL.deltaMove(shift);
		} else if (currentAction.equals(Consts.DRAWING_ACTIONS_ROTATE)) {
			if (motionEvent == MotionEvent.ACTION_DOWN) {
				drawingAction = new DrawingActionDTO(currentAction,
						currentColor, currentStrokeWidth);
				drawingPlane.addAction(drawingAction);
			} else if (motionEvent == MotionEvent.ACTION_MOVE) {

				drawingAction.deltaMove(shift);

			} else if (motionEvent == MotionEvent.ACTION_UP) {
				drawingAction.deltaMove(shift);
			}
		} else if (currentAction.equals(Consts.DRAWING_ACTIONS_SCALE)) {
			if (motionEvent == MotionEvent.ACTION_DOWN) {
				drawingAction = new DrawingActionDTO(currentAction,
						currentColor, currentStrokeWidth);
				drawingAction.move(shift);
				drawingPlane.addAction(drawingAction);
			} else if (motionEvent == MotionEvent.ACTION_MOVE) {
				drawingAction.move(shift);
			} else if (motionEvent == MotionEvent.ACTION_UP) {
				drawingAction.move(shift);
			}
		}
		invalidate();

	}

	public void setBrushThickness(int brushWidth) {
		currentStrokeWidth = brushWidth;

	}

	public void setFill(boolean fillProperty) {
		if (fillProperty)
			paint.setStyle(Style.FILL);
		else
			paint.setStyle(Style.STROKE);

	}

	public void setSelectedColor(int i) {
		paint.setColor(i);

	}

	public void setProperties(Properties currentProperties) {
		this.currentProps = currentProperties;

		paint.setColor(currentProperties.color);
		paint.setStrokeWidth(currentProperties.strokeSize);

		if (currentProperties.fill == 0) {
			paint.setStyle(Style.STROKE);
		} else {
			paint.setStyle(Style.FILL);
		}

	}

	public Properties getProperties() {
		return currentProps;
	}

	public void movePath(int Type, int t, int from, int to) {
		matrix.reset();
		px = getWidth() / 2;
		py = getHeight() / 2;

		if (t == 1) {
			int delta = 0;
			if (from > to) {
				delta = 1;
			} else if (from < to) {
				delta = -1;
			}
			if (Type == 1) {
				matrix.postTranslate(delta, 0);
			} else if (Type == 2) {
				matrix.postTranslate(0, delta);
			}
		} else if (t == 2) {

			float scale = 1;
			if (from > to) {
				scale = 1.05f;
			} else if (from < to) {
				scale = 0.95f;
			}
			if (Type == 1) {
				matrix.postScale(scale, 1, px, py);
			} else if (Type == 2) {
				matrix.postScale(1, scale, px, py);
			}
		} else if (t == 3) {

			int rotate = 1;
			if (from > to) {
				rotate = 1;
			} else if (from < to) {
				rotate = -1;
			}

			matrix.postRotate(rotate, px, py);

		}
		currentPath.transform(matrix);
	}

	public void setGridProperties(int h, int v, boolean on, boolean isSym) {
		gridH = h;
		gridV = v;
		snap = on;
		isGridSym = isSym;
	}

}
